<?php

abstract class Core_Db_Table_Abstract extends Zend_Db_Table_Abstract
{
    /**
     * __construct() - For concrete implementation of Zend_Db_Table
     *
     * @param string|array $config string can reference a Zend_Registry key for a db adapter
     *                             OR it can reference the name of a table
     * @param array|Zend_Db_Table_Definition $definition
     */
    public function __construct($config = array(), $definition = null)
    {
        if ($definition !== null && is_array($definition)) {
            $definition = new Zend_Db_Table_Definition($definition);
        }

        if (is_string($config)) {
            if (Zend_Registry::isRegistered($config)) {
                trigger_error(__CLASS__ . '::' . __METHOD__ . '(\'registryName\') is not valid usage of Zend_Db_Table, '
                    . 'try extending Zend_Db_Table_Abstract in your extending classes.',
                    E_USER_NOTICE
                    );
                $config = array(self::ADAPTER => $config);
            } else {
                // process this as table with or without a definition
                if ($definition instanceof Zend_Db_Table_Definition
                    && $definition->hasTableConfig($config)) {
                    // this will have DEFINITION_CONFIG_NAME & DEFINITION
                    $config = $definition->getTableConfig($config);
                } else {
                    $config = array(self::NAME => $config);
                }
            }
        }

        parent::__construct($config);
    }


	/**
	 * getTableAliases
	 *
	 * @return Array
	 */
	public function getTableAliases()
	{
		return array();
	}
	

	/**
	 * getRowSet
	 * @param  Array $data The array data.
	 * @return RowSet
	 */
	public function getRowSet($data)
	{
		$data  = array(
			'table'    => $this,
			'data'     => $data,
			'readOnly' => false,
			'rowClass' => $this->getRowClass(),
			'stored'   => true
		);

		$rowsetClass = $this->getRowsetClass();
		if (!class_exists($rowsetClass)) {
				require_once 'Zend/Loader.php';
				Zend_Loader::loadClass($rowsetClass);
		}
		return new $rowsetClass($data);
	}
	public function getMax($field)
	{
		$entry = $this->getAdapter()->query(
						 	 'SELECT MAX(`'.$field.'`) AS `maxField` FROM `'.$this->_name.'`')
						 ->fetchObject();
		return is_null($entry)?0:intval($entry->maxField);
	}
	public function reSort($field, $where = '', $order = '', $from = 0)
	{
		$this->getAdapter()->query('SET @rank = '.$from);
		$this->getAdapter()->query('
			UPDATE `'.$this->_name.'` SET `'.$field.'`=@rank:=@rank+1'.
			(empty($where)?'':' WHERE '.$where).
			(empty($order)?'':' ORDER BY '.$order));
		return true;
	}
	public function pagging($page = 1, $perpage = 20, $display = 5, $url = '', $select = null, $returnRowset = true)
	{
		$entries	=	array();
		if($select instanceof Zend_Db_Table)
			return array();

		$perpage 	= (intval($perpage) < 1)?1:intval($perpage);
		$display 	= (intval($display) < 1)?1:intval($display);
		$total 		= 0;
		
		if(is_array($select))
		{
			$total 	= count($select);
		}
		else
		{
			$total 	= intval($this->getAdapter()->select()->from(array('aa' => new Zend_Db_Expr('('.$select.')')), 'COUNT(*) AS t')->query()->fetchObject()->t);
		}

		$totalpages = ceil($total/$perpage);
		
		$page 		= (intval($page) > $totalpages)?$totalpages:intval($page);
		$page 		= (intval($page) < 1)?1:intval($page);

		if($totalpages <= $display)
		{
			$begin 	= 1;
			$end 		= $totalpages;
		}
		elseif($page <= ($display/2))
		{
			$begin 	= 1;
			$end 		= $display;
		}
		elseif($page >= ($totalpages-intval($display/2)))
		{
			$begin 	= $totalpages-$display+1;
			$end 		= $totalpages;
		}
		{
			$begin 	= $page-intval($display/2);
			$end 		= $begin+$display-1;
		}
		
		if(is_array($select))
		{
			$entries 	= array_slice($select, ($page-1)*$perpage, $perpage);
		}
		else
		{
			if($returnRowset)
			{
				$entries 	= $this->fetchAll($select->limit($perpage,($page-1)*$perpage))->getRows();
			}
			else
			{
				$entries 	= $select->limit($perpage,($page-1)*$perpage)->query()->fetchAll();
			}
		}

		return array(
			'perpage'				=>	$perpage,
			'display'				=>	$display,
			'total'					=>	$total,
			'totalpages'		=>	$totalpages,
			'page'					=>	$page,
			'begin'					=>	$begin,
			'end'						=>	$end,
			'url'						=>	$url,
			'entries'				=>	$entries
		);
	}
	public function getValue($ex)
	{
		return intval($this->getAdapter()->select()
			->from(array('a' => $this->info('name')), array('t' => $ex))
			->query()->fetchObject()->t);
	}
}